/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sigmacinema.service.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sigmacinema.entity.Authorities;
import sigmacinema.entity.Customer;

import sigmacinema.entity.BookingDetail;
import sigmacinema.entity.BookingHeader;
import sigmacinema.entity.Hall;
import sigmacinema.entity.Movie;
import sigmacinema.entity.PaymentDetail;
import sigmacinema.entity.PaymentHeader;
import sigmacinema.entity.Schedule;
import sigmacinema.entity.Seat;
import sigmacinema.entity.Ticket;
import sigmacinema.entity.User;
import sigmacinema.service.SigmaCinemaService;

/**
 *
 * @author sangaji
 */
@Service("SigmaCinemaService")
@Transactional(readOnly=true)
public class SigmaCinemaServiceImpl implements SigmaCinemaService{

    @Autowired private SessionFactory sessionFactory;
    
    @Override
    @Transactional(readOnly=false)
    public void saveMovie(Movie movie) {
        sessionFactory.getCurrentSession().saveOrUpdate(movie);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteMovie(Movie movie) {
            // TODO Auto-generated method stub
            sessionFactory.getCurrentSession().delete(movie);
    }

    @Override
    public List<Movie> FindAllMovie() {
            // TODO Auto-generated method stub
            return sessionFactory.getCurrentSession()
            .createQuery("FROM Movie")
            .list();
    }

    @Override
    public Movie FindMovieByKode(Integer kode) {
            // TODO Auto-generated method stub
            if (kode==null) return null;
            return (Movie) sessionFactory.getCurrentSession()
                    .createQuery("FROM Movie m WHERE m.id=:kd")
                    .setParameter("kd", kode)
                    .uniqueResult();
    }

    @Override
    public List<Hall> FindAllHall() {
            // TODO Auto-generated method stub
            return sessionFactory.getCurrentSession()
            .createQuery("FROM Hall")
            .list();
    }

    @Override
    @Transactional(readOnly=false)
    public void saveHall(Hall hall) {
            // TODO Auto-generated method stub
            sessionFactory.getCurrentSession().saveOrUpdate(hall);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteHall(Hall hall) {
            // TODO Auto-generated method stub
            sessionFactory.getCurrentSession().delete(hall);
    }

    @Override
    public Hall findHallById(String id) {
            // TODO Auto-generated method stub
            if (id==null) return null;
            return (Hall) sessionFactory.getCurrentSession()
                    .createQuery("FROM Hall h WHERE h.id=:kd")
                    .setParameter("kd", id)
                    .uniqueResult();
    }

    @Override
    public List<Seat> findAllSeat() {
            // TODO Auto-generated method stub
            return sessionFactory.getCurrentSession()
            .createQuery("FROM Seat")
            .list();
    }

    @Override
    @Transactional(readOnly=false)
    public void saveSeat(Seat seat) {
            // TODO Auto-generated method stub
            sessionFactory.getCurrentSession().saveOrUpdate(seat);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteSeat(Seat seat) {
            // TODO Auto-generated method stub
            sessionFactory.getCurrentSession().delete(seat);
    }

	@Override
	public List<Schedule> findAllSchedule() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession()
		.createQuery("FROM Schedule")
		.list();
	}
	
    @Override
    @Transactional(readOnly=false)
	public void saveSchedule(Schedule schedule) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().saveOrUpdate(schedule);
		
	}
    
	@Override
	@Transactional(readOnly=false)
	public void deleteSchedule(Schedule schedule) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().delete(schedule);
	}
	
	
	@Override
	public Schedule findScheduleById(Integer id) {
		// TODO Auto-generated method stub
		if (id==null) return null;
        return (Schedule) sessionFactory.getCurrentSession()
                .createQuery("FROM Schedule s WHERE s.id=:kd")
                .setParameter("kd", id)
                .uniqueResult();
	}

	
	@Override
	public List<Ticket> findAllTicket() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession()
		.createQuery("FROM Ticket")
		.list();
	}
	
	
	@Override
	@Transactional(readOnly=false)
	public void saveTicket(Ticket ticket) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().saveOrUpdate(ticket);
		findAllTicket();
	}
	
	@Override
	public void deleteTicket(Ticket ticket) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().delete(ticket);
	}

	
	@Override
	public Ticket findTicketById(Integer id) {
		// TODO Auto-generated method stub
		if (id==null) return null;
        return (Ticket) sessionFactory.getCurrentSession()
                .createQuery("FROM Ticket t WHERE t.id=:kd")
                .setParameter("kd", id)
                .uniqueResult();
	}

	@Override
    public Seat findSeatById(Integer id) {
            // TODO Auto-generated method stub
            if (id==null) return null;
            return (Seat) sessionFactory.getCurrentSession()
                    .createQuery("FROM Seat s WHERE s.id=:kd")
                    .setParameter("kd", id)
                    .uniqueResult();
    }
        
    @Override
    @Transactional(readOnly=false)
    public void saveCustomer(User user) {
        sessionFactory.getCurrentSession().saveOrUpdate(user);
    }
    
    @Override
    @Transactional(readOnly=false)
    public void delCustomer(Customer customer) {
        sessionFactory.getCurrentSession().delete(customer);
    }
    
    
    @Override
	public Customer findCustomerById(Integer id) {
		// TODO Auto-generated method stub
    	if (id==null) return null;
        return (Customer) sessionFactory.getCurrentSession()
                .createQuery("FROM Customer c WHERE c.id=:kd")
                .setParameter("kd", id)
                .uniqueResult();
	}

	@Override
    public List<Customer> findAllCustomer() {
        return sessionFactory.getCurrentSession().createQuery("FROM Customer").list();
    }
    
    @Override
    public List<Customer> findCustomerByName(String name) {
        return sessionFactory.getCurrentSession().createQuery("FROM Customer c where c.custName=:name")
                .setParameter("name", name).list();
    }
    
    @Override
    public User findUserByUsername(String username) {
        return (User) sessionFactory.getCurrentSession().createQuery("FROM User u where u.username=:user")
                .setParameter("user", username).uniqueResult();
    }

    @Override
    @Transactional(readOnly=false)
    public void saveAuthorities(Authorities auth) {
        sessionFactory.getCurrentSession().saveOrUpdate(auth);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteAuthorities(Authorities auth) {
        sessionFactory.getCurrentSession().delete(auth);
    }

	@Override
	public List<BookingHeader> findAllBH() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession()
		.createQuery("FROM BookingHeader")
		.list();
	}

	@Override
	@Transactional(readOnly=false)
	public void saveBooking(BookingHeader bookingHeader) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().saveOrUpdate(bookingHeader);
	}

	@Override
	public BookingHeader findBhById(String id) {
		// TODO Auto-generated method stub
		if (id==null) return null;
        return (BookingHeader) sessionFactory.getCurrentSession()
                .createQuery("FROM BookingHeader bh WHERE bh.bookingID=:kd")
                .setParameter("kd", id)
                .uniqueResult();
	}

	@Override
	public List<BookingDetail> findBdetailByIdBheader(String id) {
		// TODO Auto-generated method stub
		if (id==null) return null;
		return sessionFactory.getCurrentSession()
		.createQuery("FROM BookingHeader bh " +
					" INNER JOIN bh.BookingDetail bk " +
					" WHERE bh.bookingID=:kd")
		.setParameter("kd", id)
		.list();
	}

	@Override
	public List<PaymentHeader> findAllPayH() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession()
		.createQuery("FROM PaymentHeader")
		.list();
	}

	@Override
	@Transactional(readOnly=false)
	public void savePayment(PaymentHeader paymentHeader) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().saveOrUpdate(paymentHeader);
	}

	@Override
	public PaymentHeader findPayHById(String id) {
		// TODO Auto-generated method stub
		if (id==null) return null;
        return (PaymentHeader) sessionFactory.getCurrentSession()
                .createQuery("FROM PaymentHeader ph WHERE ph.paymentNo=:kd")
                .setParameter("kd", id)
                .uniqueResult();
	}

	@Override
	public List<PaymentDetail> findPayDByPayHId(String id) {
		// TODO Auto-generated method stub
		if (id==null) return null;
		return sessionFactory.getCurrentSession()
		.createQuery("FROM PaymentHeader ph " +
					" INNER JOIN ph.PaymentDetail pk " +
					" WHERE ph.paymentNo=:kd")
		.setParameter("kd", id)
		.list();
	}
	
}
